from django import http
from django.conf import settings
from django.contrib.auth import login
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View

from apps.ausers.models import User
from apps.oauth.models import OAuthQQUser
from meiduo_mall.settings.dev import logger
from utils.response_code import RETCODE
from utils.secret import SecretOauth


def is_bind_openid(openid, request, next):
    try:
        oauth_user = OAuthQQUser.objects.get(openid=openid)
    except Exception as e:
        logger.error(e)
        # 2.未绑定了   ---跳转到 绑定的页面
        data_dict = {'openid' : openid}
        secret_openid = SecretOauth().dumps(data_dict)
        context = {'openid': openid}
        return render(request, 'oauth_callback.html', context)

    else:
        # 1.绑定 --保持登录状态---设置username--cookie--跳转到首页
        user = oauth_user.user
        #   保持登录状态
        login(request, user)
        #   ---设置username--cookie
        if next:
            response = redirect(next)
        else:
            response = redirect(reverse('contents:index'))
        # --跳转到首页
        response.set_cookie('username', user.username, max_age=3600 * 24 * 14)

        return response
# 2.QQ 登录--code-->token-->openid
# http://www.meiduo.site:8000/oauth_callback?code=514D2BE5E31DBC30106501CDC03ACD0A&state=None
class QQAuthUserView(View):
    """用户扫码登录的回调处理"""
    def get(self, request):
        """Oauth2.0认证"""
        code = request.GET.get('code')
        next = request.GET.get('state')
        if not code:
            return http.HttpResponseForbidden('缺少code')

        from QQLoginTool.QQtool import OAuthQQ
        oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET,
                        redirect_uri=settings.QQ_REDIRECT_URI, state=next)

        try:
            # code-->token
            access_token = oauth.get_access_token(code)
            # token--->openid
            openid = oauth.get_open_id(access_token)
        except Exception as e:
            logger.error(e)
            return http.HttpResponseServerError('Oauth2.0认证失败!')

        # 判断是否绑定了openid
        response = is_bind_openid(openid, request, next)
        # 返回前端
        return response

    def post(self, request):
        # 1.接收参数　　reqeust.POST
        mobile = request.POST.get('mobile')
        pwd = request.POST.get('password')
        sms_code_client = request.POST.get('sms_code')
        openid = request.POST.get('openid')
        # 2.解析参数　　校验　判空　正则
        # 校验参数
        # # 判断参数是否齐全
        # if not all([mobile, pwd, sms_code_client]):
        #     return http.HttpResponseForbidden('缺少必传参数')
        # # 判断手机号是否合法
        # if not re.match(r'^1[3-9]\d{9}$', mobile):
        #     return http.HttpResponseForbidden('请输入正确的手机号码')
        # # 判断密码是否合格
        # if not re.match(r'^[0-9A-Za-z]{8,20}$', pwd):
        #     return http.HttpResponseForbidden('请输入8-20位的密码')
        # # 3.校验短信　ｒｅｄｉｓ取出来　　redis_sms_decode()
        # redis_conn = get_redis_connection('verify_code')
        # sms_code_server = redis_conn.get('sms_%s' % mobile)
        # if sms_code_server is None:
        #     return render(request, 'oauth_callback.html', {'sms_code_errmsg': '无效的短信验证码'})
        # if sms_code_client != sms_code_server.decode():
        #     return render(request, 'oauth_callback.html', {'sms_code_errmsg': '输入短信验证码有误'})

        # 校验openid 有还是没有
        # 解密 openid
        openid = SecretOauth().loads('openid', None)
        if not openid:
            return render(request, 'oauth_callback.html', {'openid_errmsg': '无效的openid'})
        # 4.判断用户是否存在　mobile=mobile
        # 保存注册数据
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 用户不存在,新建用户
            user = User.objects.create_user(username=mobile, password=pwd, mobile=mobile)
        else:
            # 如果用户存在，检查用户密码
            if not user.check_password(pwd):
                return render(request, 'oauth_callback.html', {'account_errmsg': '用户名或密码错误'})

        # 　５．绑定用户　openid和user绑定　－－新建一条记录
        try:
            OAuthQQUser.objects.create(
            openid=openid,
            user=user)
        except Exception as e:
            logger.error(e)
            return render(request, 'oauth_callback.html', {'qq_login_errmsg': 'QQ登录失败'})
        # 6.login() -set_cookie 重定向的首页
        # 保持登录状态　
        login(request, user)

        next = request.GET.get('state')
        if next:
            response = redirect(next)
        else:
        # 响应绑定结果
            response = redirect(reverse('contents:index'))
        # 登录时用户名写入到cookie，有效期15天
        response.set_cookie('username', user.username, max_age=3600 * 24 * 15)

        return response


# 1.获取登录网址
class QQAuthURLView(View):
    def get(self, request):
        next = request.GET.get('next')
        # 1.实例化 qq认证类
        from QQLoginTool.QQtool import OAuthQQ
        oauth_qq = OAuthQQ(
            client_secret=settings.QQ_CLIENT_SECRET,
            client_id=settings.QQ_CLIENT_ID,
            redirect_uri=settings.QQ_REDIRECT_URI,
            state=next
        )

        # 2.获取登录网址
        login_url = oauth_qq.get_qq_url()

        # 3.返回给前端
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'login_url': login_url})

